<template>
  <div class="file-upload-demo">
    <h1>大文件上传示例</h1>
    
    <section class="demo-section">
      <h2>基本用法 - 带有类型和大小验证</h2>
      <p class="section-desc">上传组件会在上传前验证文件类型和大小，并提供预览功能</p>
      
      <FileUploader 
        @file-selected="onFileSelected" 
        @file-validated="onFileValidated" 
        @validation-failed="onValidationFailed"
        @upload-success="onUploadSuccess"
      />
    </section>
    
    <section class="demo-section">
      <h2>自定义验证规则</h2>
      <p class="section-desc">限制只能上传图片，最大5MB</p>
      
      <FileUploader 
        fileTypeLimit="image" 
        :maxFileSize="5 * 1024 * 1024" 
        :beforeUpload="customValidator"
      />
    </section>
    
    <section class="demo-section">
      <h2>只允许视频文件</h2>
      <p class="section-desc">限制只能上传视频文件，最大100MB</p>
      
      <FileUploader 
        fileTypeLimit="video" 
        :maxFileSize="100 * 1024 * 1024" 
      />
    </section>
    
    <section class="demo-section">
      <h2>事件日志</h2>
      <div class="log-container">
        <div v-if="logs.length === 0" class="no-logs">暂无日志</div>
        <div v-for="(log, index) in logs" :key="index" class="log-item" :class="log.type">
          <span class="log-time">{{ log.time }}</span>
          <span class="log-message">{{ log.message }}</span>
        </div>
      </div>
      <button @click="clearLogs" class="clear-logs-btn">清除日志</button>
    </section>
  </div>
</template>

<script setup lang="ts">
// 导入FileUploader组件
import { defineAsyncComponent } from 'vue';
import { ref } from 'vue';

// 异步导入组件
const FileUploader = defineAsyncComponent(() => import('../../components/FileUploader.vue'));

// 定义日志记录类型
interface LogItem {
  time: string;
  message: string;
  type: 'info' | 'success' | 'error' | 'warning';
}

// 日志记录
const logs = ref<LogItem[]>([]);

// 添加日志
function addLog(message: string, type: 'info' | 'success' | 'error' | 'warning' = 'info') {
  // 获取当前时间
  const now = new Date();
  const time = `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}:${now.getSeconds().toString().padStart(2, '0')}`;
  
  // 添加到日志列表
  logs.value.push({ time, message, type });
  
  // 保留最多30条日志
  if (logs.value.length > 30) {
    logs.value.shift();
  }
}

// 清除日志
function clearLogs() {
  logs.value = [];
}

// 文件选择事件处理
function onFileSelected(file: File) {
  addLog(`文件已选择: ${file.name} (${formatFileSize(file.size)}, ${file.type})`, 'info');
}

// 文件验证通过事件处理
function onFileValidated(file: File) {
  addLog(`文件验证通过: ${file.name}`, 'success');
}

// 验证失败事件处理
function onValidationFailed(data: { file: File, reason: string, message: string }) {
  addLog(`文件验证失败: ${data.message}`, 'error');
}

// 上传成功事件处理
function onUploadSuccess(data: { file: File, response: any }) {
  addLog(`文件上传成功: ${data.file.name}`, 'success');
}

// 自定义验证函数 - 演示如何添加自定义验证规则
function customValidator(file: File): boolean {
  // 示例：只允许文件名以"fitness_"开头的文件
  const valid = file.name.startsWith('fitness_');
  
  // 如果验证失败，记录日志
  if (!valid) {
    addLog(`自定义验证失败: 文件名必须以"fitness_"开头`, 'warning');
  }
  
  return valid;
}

// 格式化文件大小，与FileUploader组件中的函数相同
function formatFileSize(bytes: number): string {
  if (bytes < 1024) {
    return bytes + ' B';
  } else if (bytes < 1024 * 1024) {
    return (bytes / 1024).toFixed(1) + ' KB';
  } else if (bytes < 1024 * 1024 * 1024) {
    return (bytes / (1024 * 1024)).toFixed(1) + ' MB';
  } else {
    return (bytes / (1024 * 1024 * 1024)).toFixed(1) + ' GB';
  }
}
</script>

<style scoped>
.file-upload-demo {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
}

h1 {
  font-size: 24px;
  margin-bottom: 24px;
  color: #333;
  text-align: center;
}

.demo-section {
  margin-bottom: 40px;
  padding: 20px;
  border: 1px solid #e8e8e8;
  border-radius: 8px;
  background-color: #fff;
}

h2 {
  font-size: 18px;
  margin-bottom: 8px;
  color: #333;
}

.section-desc {
  margin-bottom: 16px;
  font-size: 14px;
  color: #666;
}

.log-container {
  height: 300px;
  overflow-y: auto;
  border: 1px solid #e8e8e8;
  border-radius: 4px;
  padding: 8px;
  margin-bottom: 16px;
  background-color: #f8f8f8;
}

.no-logs {
  color: #999;
  text-align: center;
  padding: 20px;
}

.log-item {
  padding: 6px 8px;
  border-bottom: 1px solid #eee;
  font-size: 14px;
  display: flex;
  align-items: flex-start;
}

.log-item:last-child {
  border-bottom: none;
}

.log-time {
  flex-shrink: 0;
  margin-right: 12px;
  color: #999;
  font-family: monospace;
}

.log-message {
  flex-grow: 1;
  word-break: break-word;
}

.info {
  background-color: #e6f7ff;
}

.success {
  background-color: #f6ffed;
}

.error {
  background-color: #fff2f0;
}

.warning {
  background-color: #fffbe6;
}

.clear-logs-btn {
  padding: 6px 12px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  background-color: white;
  cursor: pointer;
  transition: all 0.3s;
  font-size: 14px;
  color: #ff4d4f;
  border-color: #ff4d4f;
}

.clear-logs-btn:hover {
  background-color: rgba(255, 77, 79, 0.1);
}
</style> 